/*
 *
 * Copyright Eclub organization.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at

 * http://www.apache.org/licenses/LICENSE-2.0

 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.eclub.emotion.rootstone.protocol.enumeration;

import com.eclub.emotion.rootstone.protocol.sdp.AuthenticationProtocol;
import com.eclub.emotion.rootstone.protocol.sdp.ExceptionProtocol;
import com.eclub.emotion.rootstone.protocol.sdp.RequestProtocol;
import com.eclub.emotion.rootstone.protocol.sdp.ResetProtocol;
import com.eclub.emotion.rootstone.protocol.sdp.ResponseProtocol;

/**
 * 
 * Session description protocol
 * 
 * @author jun.yang, Wincor Nixdorf International GmbH
 * @version $Revision$
 */
public enum SDPType {
	
	Response(1), 
	Request(2), 
	Exception(3), 
	Config(4), 
	Authentication(5), 
	Reset(6);

	private final int num;

	public int getNum() {
		return num;
	}

	private SDPType(int num) {
		this.num = num;
	}

	public static SDPType getSDPType(int num) throws java.lang.Exception {
		for (SDPType type : SDPType.values()) {
			if (type.getNum() == num) {
				return type;
			}
		}
		throw new Exception("Unknown Session Description Protocol:" + num);
	}

	/**
	 * 
	 * @param clazz
	 * @return
	 * @throws Exception
	 */
	public static SDPType getSDPType(Class<?> clazz) throws Exception {
		if (clazz == RequestProtocol.class) {
			return SDPType.Request;
		} else if (clazz == ResponseProtocol.class) {
			return SDPType.Response;
		} else if (clazz == ExceptionProtocol.class) {
			return SDPType.Exception;
		} else if (clazz == AuthenticationProtocol.class) {
			return SDPType.Authentication;
		} else if (clazz == ResetProtocol.class) {
			return SDPType.Reset;
		}
		throw new Exception("Unknown Session Description Protocol:" + clazz.getName());
	}

	/**
	 * 
	 * @param type
	 * @return
	 * @throws Exception
	 */
	public static Class<?> getSDPClass(SDPType type) throws Exception {
		if (type == SDPType.Request) {
			return RequestProtocol.class;
		} else if (type == SDPType.Response) {
			return ResponseProtocol.class;
		} else if (type == SDPType.Exception) {
			return ExceptionProtocol.class;
		} else if (type == SDPType.Authentication) {
			return AuthenticationProtocol.class;
		} else if (type == SDPType.Reset) {
			return ResetProtocol.class;
		}
		throw new Exception("Unknown Session Description Protocol:" + type);
	}

	public static SDPType getSDPType(Object obj) {
		if (obj instanceof RequestProtocol) {
			return SDPType.Request;
		} else if (obj instanceof ResponseProtocol) {
			return SDPType.Response;
		} else if (obj instanceof AuthenticationProtocol) {
			return SDPType.Authentication;
		} else if (obj instanceof ResetProtocol) {
			return SDPType.Reset;
		} else {
			return SDPType.Exception;
		}
	}
}
